package org.example.physics_engine.geometry.shapes;

import lombok.Getter;
import lombok.Setter;
import org.example.physics_engine.body.Mass;
import org.example.physics_engine.body.Transform;
import org.example.physics_engine.geometry.*;

@Getter
@Setter
public class Circle extends AbstractShape{

    //圆的半径
    protected double radius;

    public Circle(double radius, Transform transform) {
        super(transform);
        this.radius = radius;
    }

    public Circle(double radius) {
        this(radius, new Transform());
    }

    //反序列化调用
    public Circle() {
        //单位圆
        this(1);
    }
    @Override
    public void zoom(int rate) {
        if (rate < 1) return;
        this.radius = this.radius / this.rate * rate;
        this.rate = rate;
    }

    @Override
    public AxisAlignBoundingBox createAABB() {
        return new AxisAlignBoundingBox(transform.getX() - radius,
                transform.getY() - radius,
                transform.getX() + radius,
                transform.getY() + radius);
    }

    @Override
    public Interval project(Vector2d axis) {
        Vector2d center = new Vector2d(transform.getX(), transform.getY());
        double c = center.dotProduct(axis);
        return new Interval(c - radius, c + radius);
    }

    @Override
    public Mass createMass(double density) {
        double r2 = this.radius * this.radius;
        double mass = density * Math.PI * r2;
        double inertia = mass * r2 * 0.5;
        return new Mass(new Vector2d(0, 0), mass, inertia);
    }

    @Override
    public Vector2d[] getFocus() {
        Vector2d[] focus = new Vector2d[1];
        focus[0] = new Vector2d(transform.getX(), transform.getY());
        return focus;
    }

    @Override
    public Vector2d[] getAxes(Vector2d[] focus) {
        //都是其他图像根据圆的焦点获取分离轴
        return null;
    }

    @Override
    public Vector2d getFarthestFeature(Vector2d vector) {
        return this.getFarthestPoint(vector);
    }

    @Override
    public Vector2d getFarthestPoint(Vector2d vector) {
        Vector2d nAxis = vector.getNormalized();
        Vector2d center = new Vector2d(transform.getX(), transform.getY());
        center.x += this.radius * nAxis.x;
        center.y += this.radius * nAxis.y;
        return center;
    }
}
